package com.wechat.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.wechat.annotation.Auth;
import com.wechat.apidoc.background.WxBackgroundInterface;
import com.wechat.entity.Question;
import com.wechat.entity.QuestionLib;
import com.wechat.entity.User;
import com.wechat.entity.extend.AnswerDetailExt;
import com.wechat.entity.extend.AnswerExt;
import com.wechat.entity.extend.QuestionExt;
import com.wechat.entity.extend.UserWithdrawcashRecordsExt;
import com.wechat.service.BackgroundService;
import com.wechat.service.RecordService;
import com.wechat.utils.ActionResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by ycx on 2018/4/16.
 */
@RestController
@RequestMapping(value = "api/v1")
public class WxBackgroundController implements WxBackgroundInterface {

    private static Logger logger = LoggerFactory.getLogger(WxBackgroundController.class);

    @Autowired
    private RecordService recordService;
    @Autowired
    private BackgroundService backgroundService;

    /**
     * 题库管理
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/question_lib_list", method = RequestMethod.POST)
    public ActionResult questionLibList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        logger.info("[题库管理接口]:{}", "pageNum=" + pageNum + ",pageSize=" + pageSize);

        ActionResult result = new ActionResult<>();

        try {
            List<QuestionLib> questionLibList = backgroundService.questionLibList(pageNum, pageSize);

            PageInfo<QuestionLib> pageInfo = new PageInfo<>(questionLibList);

            result.setData(pageInfo);
            result.setSuccess(true);
            result.setMessage("查询题库成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询题库失败");

            logger.warn("[题库管理接口异常]：", e);
        }

        return result;
    }

    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/question_lib/save", method = RequestMethod.POST)
    public ActionResult questionLibAdd(QuestionLib record) {
        logger.info("[题库添加接口]:{}", JSONObject.toJSONString(record));

        ActionResult result = new ActionResult<>();

        try {

            backgroundService.questionSave(record);

            result.setSuccess(true);
            result.setMessage("添加成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("添加失败");

            logger.warn("[题库管理接口异常]：", e);
        }

        return result;
    }

    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/question_lib/edit", method = RequestMethod.GET)
    public ActionResult questionLibEdit(Integer questionLibId) {
        logger.info("[题库编辑接口]:{}", JSONObject.toJSONString(questionLibId));

        ActionResult result = new ActionResult<>();

        try {

            QuestionLib questionLib = backgroundService.questionEdit(questionLibId);

            result.setSuccess(true);
            result.setData(questionLib);
            result.setMessage("编辑成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("编辑失败");

            logger.warn("[题库管理接口异常]：", e);
        }

        return result;
    }

    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/question_lib/update", method = RequestMethod.POST)
    public ActionResult questionLibUpdate(QuestionLib record) {
        logger.info("[题库添加更新]:{}", JSONObject.toJSONString(record));

        ActionResult result = new ActionResult<>();

        try {

            backgroundService.questionUpdate(record);

            result.setSuccess(true);
            result.setMessage("更新成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("更新失败");

            logger.warn("[题库管理接口异常]：", e);
        }

        return result;
    }

    /**
     * 生成试卷(指定题目数)
     *
     * @param num
     * @return
     */
    @Auth(user = "admin")
    @RequestMapping(value = "background/question/save", method = RequestMethod.POST)
    public ActionResult questionSave(Integer num) {
        ActionResult result = new ActionResult();

        return result;
    }

    /**
     * 提现记录
     *
     * @param pageNum
     * @param pageSize
     * @param nickname
     * @param isDesc   0-升序 1-降序
     * @return
     */
    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/withdrawcash_records_list", method = RequestMethod.POST)
    public ActionResult withdrawcashRecordsList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String nickname, Integer isDesc) {
        logger.info("[提现记录接口]:{}", "pageNum=" + pageNum + ",pageSize=" + pageSize + ",nickname=" + nickname + ",isDesc=" + isDesc);

        ActionResult result = new ActionResult<>();

        try {
            List<UserWithdrawcashRecordsExt> userWithdrawcashRecordsExtlist = backgroundService.userWithdrawcashRecordsList(pageNum, pageSize, nickname, isDesc);

            PageInfo<UserWithdrawcashRecordsExt> pageInfo = new PageInfo<>(userWithdrawcashRecordsExtlist);

            result.setData(pageInfo);
            result.setSuccess(true);
            result.setMessage("查询提现记录表成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询提现记录表失败");

            logger.warn("[提现记录接口异常]：", e);
        }

        return result;
    }

    /**
     * 出题记录
     *
     * @param pageNum
     * @param pageSize
     * @param nickname
     * @param id
     * @param isDesc   0-升序 1-降序
     * @return
     */
    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/question_list", method = RequestMethod.POST)
    public ActionResult questionList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String nickname, Integer id, Integer isDesc) {
        logger.info("[出题记录接口]:{}", "pageNum=" + pageNum + ",pageSize=" + pageSize + ",nickname=" + nickname + ",isDesc=" + isDesc + ",id=" + id);

        ActionResult result = new ActionResult<>();

        try {
            List<QuestionExt> questionExtList = backgroundService.questionList(pageNum, pageSize, nickname, id, isDesc);

            PageInfo<QuestionExt> pageInfo = new PageInfo<>(questionExtList);

            result.setData(pageInfo);
            result.setSuccess(true);
            result.setMessage("查询出题记录表成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询出题记录表失败");

            logger.warn("[出题记录接口异常]：", e);
        }

        return result;
    }

    /**
     * 用户管理
     *
     * @param pageNum
     * @param pageSize
     * @param nickname
     * @param sort     0-出题记录 1-答题记录 2-注册日期
     * @param isDesc   0-升序 1-降序
     * @return
     */
    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/user_list", method = RequestMethod.POST)
    public ActionResult userList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String nickname, Integer sort, Integer isDesc) {
        logger.info("[用户管理接口]:{}", "pageNum=" + pageNum + ",pageSize=" + pageSize + ",nickname=" + nickname + ",isDesc=" + isDesc + ",sort=" + sort);

        ActionResult result = new ActionResult<>();

        try {
            List<User> userList = backgroundService.userList(pageNum, pageSize, nickname, sort, isDesc);

            PageInfo<User> pageInfo = new PageInfo<>(userList);

            result.setData(pageInfo);
            result.setSuccess(true);
            result.setMessage("查询用户表成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询用户表失败");

            logger.warn("[用户管理接口异常]：", e);
        }

        return result;
    }

    /**
     * 查看题目
     *
     * @param questionId
     * @return
     */
    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/see_question", method = RequestMethod.POST)
    public ActionResult seeQuestion(Integer questionId) {
        logger.info("[查看题目接口]:{}", "questionId=" + questionId);

        ActionResult result = new ActionResult<>();

        if (questionId == null || questionId == 0) {
            result.setSuccess(false);
            result.setMessage("参数不正确");
            return result;
        }

        try {
            List<QuestionLib> questionLibList = recordService.findQuestionLibList(questionId);


            result.setData(questionLibList);
            result.setSuccess(true);
            result.setMessage("查看题目成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查看题目失败");

            logger.warn("[查看题目接口异常]：", e);
        }

        return result;
    }

    /**
     * 答题记录
     *
     * @param pageNum
     * @param pageSize
     * @param answerPerson
     * @param questionPerson
     * @param questionId
     * @param isDesc         0-升序 1-降序
     * @return
     */
    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/answer_list", method = RequestMethod.POST)
    public ActionResult answerList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String answerPerson, String questionPerson, Integer questionId, Integer isDesc) {
        logger.info("[答题记录接口]:{}", "pageNum=" + pageNum + ",pageSize=" + pageSize + ",answerPerson=" + answerPerson + ",isDesc=" + isDesc + ",questionId=" + questionId + ",questionPerson=" + questionPerson);

        ActionResult result = new ActionResult<>();

        try {
            List<AnswerExt> answerExtList = backgroundService.answerList(pageNum, pageSize, answerPerson, questionPerson, questionId, isDesc);

            PageInfo<AnswerExt> pageInfo = new PageInfo<>(answerExtList);

            result.setData(pageInfo);
            result.setSuccess(true);
            result.setMessage("查询答题记录表成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询答题记录表失败");

            logger.warn("[答题记录接口异常]：", e);
        }

        return result;
    }

    /**
     * 查看答题情况
     *
     * @param answerId
     * @return
     */
    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/see_answer", method = RequestMethod.POST)
    public ActionResult seeAnswer(Integer answerId) {
        logger.info("[查看答题情况接口]:{}", "answerId=" + answerId);

        ActionResult result = new ActionResult<>();

        if (answerId == null || answerId == 0) {
            result.setSuccess(false);
            result.setMessage("参数不正确");
            return result;
        }

        try {
            List<AnswerDetailExt> answerDetailExtList = recordService.findAnswerDetailExt(answerId);

            result.setData(answerDetailExtList);
            result.setSuccess(true);
            result.setMessage("查看答题情况成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查看答题情况失败");

            logger.warn("[查看答题情况接口异常]：", e);
        }

        return result;
    }

    /**
     * 红包发放概况
     *
     * @param id
     * @return
     */
    @Override
    @Auth(user = "admin")
    @RequestMapping(value = "background/see_red_packet", method = RequestMethod.POST)
    public ActionResult seeRedPpacket(Integer id) {
        logger.info("[红包发放概况接口]:{}", "id=" + id);

        ActionResult result = new ActionResult<>();

        if (id == null || id == 0) {
            result.setSuccess(false);
            result.setMessage("参数不正确");
            return result;
        }

        try {
            Question question = backgroundService.selById(id);
            double redPacket = backgroundService.selSumRedPacketByQuestionId(id);

            if (question.getAmountStatus() == Question.AMOUNT_STATUS_RETURN) {
                result.put("returnAmount", question.getTotalAmount().doubleValue() - redPacket);
            }

            result.setData(question);
            result.put("redPacket", redPacket);
            result.put("number", question.getTotalNumber() - question.getSurplusNumber());
            result.setSuccess(true);
            result.setMessage("查看红包发放概况成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查看红包发放概况失败");

            logger.warn("[红包发放概况接口异常]：", e);
        }

        return result;
    }

}


























































